﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using WcfServer.Classes.Client;
using WcfServer.Classes.Utility;


namespace Client.ViewModel
{
    class ChatViewModel :ViewModelBase
    {
        Socket socket;
        byte[] SessionKey;
        string friendUserName;
        public ChatViewModel(Socket soc,  byte[] sessionkey,string fUN)
        {
            socket = soc;
            SessionKey = sessionkey;
            friendUserName = fUN;
            Task.Factory.StartNew(delegate
            {
                while (true)
                {
                    //Thread.Sleep(300);

                    byte[] buffer = new byte[10000]; //TODO: is 10KB enough?
                    int bytesReceived = socket.Receive(buffer);

                    byte[] data = new byte[bytesReceived];
                    Buffer.BlockCopy(buffer, 0, data, 0, bytesReceived);

                    PersonalMessage pm = new PersonalMessage(data, SessionKey);
                    //string friendPublicKeyXML = KeyHandler.readPublicXML(friendUserName);
                    //TO ARMAN:
                    //we need something like below line for validation but I don't know if != works (since they are references. we want to hash the message and compare it too the signed hash which is decryptedRSA first
                    //if (EncryptionHandler.DecryptRSA_XML(pm.signedMessageChecksum, friendPublicKeyXML) != EncryptionHandler.HashSHA256(SerializerHandler<string>.SerializeInput(pm.PMessage))) ;


                    //MessageBox.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                    //    (Action)(() => MessageBox.Text += "\n" + pm.SenderUserID + " (" + pm.TimeStamp + "): " + pm.PMessage));
                    Conversation += "\n" + pm.SenderUserID + " (" + pm.TimeStamp + "): " + pm.PMessage;
                }
            });
        }
        private string conversation;
        public string Conversation
        {
            set
            {
                if (conversation != value)
                {
                    conversation = value;
                    base.OnPropertyChanged("Conversation");
                }
            }
            get
            {
                return conversation;
            }
        }
        private string message;
        public string Message
        {
            set
            {
                if(message!=value)
                {
                    message = value;
                    base.OnPropertyChanged("Message");
                }
            }
            get
            {
                return message;
            }
        }
        public ICommand sendCommand;
        public ICommand SendCommand
        {
            get
            {
                if (sendCommand == null)
                {
                    sendCommand = new RelayCommand(param => this.SendMessages(), param => this.CanSendMessage);
                }
                return sendCommand;
            }
        }
        public void SendMessages()
        {
            if (Message == "")
                return;

            PersonalMessage pm = new PersonalMessage()
            {
                PMessage = Message,
                SenderUserID = MainWindow.CurrentUserName,
                TimeStamp = DateTime.Now
            };

            Task.Factory.StartNew(delegate
            {
                socket.Send(pm.Send(SessionKey));
            }).ContinueWith(delegate
            {
                Conversation += "\n" + pm.SenderUserID + " (" + pm.TimeStamp + "): " + pm.PMessage;
                Message = "";
            }, TaskScheduler.FromCurrentSynchronizationContext());


        }
        private bool CanSendMessage
        {
            get { return true; }
        }
    }

}
